package com.byteshake.common.base

import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

open class BaseRepository {

    private val gson = Gson()

    protected fun mapToBody(parameter: HashMap<String, Any>): RequestBody {
        return gson.toJson(parameter).toRequestBody("application/json; charset=utf-8".toMediaType())
    }

    suspend fun <T : Any> executeRequest(block: suspend () -> BaseData<T>): BaseData<T> {
        val baseData = block.invoke()

//        if (baseData.code == 200) {
//            //正确
            baseData.state = ReqState.Success
//        } else {
//            baseData.state = ReqState.Error
//        }
        return baseData
    }
}